package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.construction.api.CategoryApi;
import com.ruyuan.eshop.construction.domain.dto.CategoryInfoDTO;
import com.ruyuan.eshop.construction.domain.dto.CategoryInfoTreeDTO;
import com.ruyuan.eshop.construction.domain.dto.CategoryResultDTO;
import com.ruyuan.eshop.construction.domain.request.QueryCategoryListRequest;
import com.ruyuan.eshop.construction.domain.request.QueryCategoryRequest;
import com.ruyuan.eshop.construction.domain.request.CategoryRequest;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.service.CategoryInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 品类服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = CategoryApi.class, retries = 0)
public class CategoryApiImpl implements CategoryApi {

    @Autowired
    private CategoryInfoService categoryInfoService;

    @Override
    public JsonResult<CategoryResultDTO> category(CategoryRequest request) {
        try {
            CategoryResultDTO categoryResultDTO =
                    categoryInfoService.saveCategory(request);
            return JsonResult.buildSuccess(categoryResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<CategoryInfoTreeDTO>> categoryInfoTree(QueryCategoryRequest request) {
        try {
            List<CategoryInfoTreeDTO> categoryInfoTreeDTOS = categoryInfoService.selectTree(request);
            return JsonResult.buildSuccess(categoryInfoTreeDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<CategoryInfoDTO>> categoryInfoChild(QueryCategoryRequest request) {
        try {
            List<CategoryInfoDTO> categoryInfoDTOS = categoryInfoService.selectChild(request);
            return JsonResult.buildSuccess(categoryInfoDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<CategoryInfoDTO>> listCategoryAll(QueryCategoryListRequest request) {
        try {
            List<CategoryInfoDTO> categoryInfoList = categoryInfoService.selectListByLike(request);
            return JsonResult.buildSuccess(categoryInfoList);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
